package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.common.exception.RepositoryException;
import com.alibaba.citrus.cr.platform.enhance.repository.OrganizationRelationRepository;
import com.alibaba.cz.base.tool.StringUtil;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crplatformenhance.domain.organizationrelation.dto.CreateOrganizationRelationResponse;
import com.epoch.app.crplatformenhance.domain.organizationrelation.dto.LoadOrganizationRelationListRequest;
import com.epoch.app.crplatformenhance.domain.organizationrelation.dto.UpdateOrganizationRelationResponse;
import com.epoch.app.crplatformenhance.domain.organizationrelation.model.OrganizationRelation;
import com.epoch.app.crplatformenhance.domain.organizationrelation.service.OrganizationRelationService;
import com.epoch.app.crplatformenhance.dto.request.IdQueryRequest;
import com.epoch.app.crplatformenhance.enums.CommStatusEnum;
import com.epoch.app.crplatformenhance.enums.OrganizationRelevanceTypeEnum;
import com.epoch.app.crplatformenhance.model.dto.SuperiorSalesOrganizationRelationLoadListRequest;
import com.epoch.app.crplatformenhance.sdo.OrganizationRelationSDO;
import com.epoch.app.crplatformenhance.superiorsalesorganizationrelation.model.SuperiorSalesOrganizationRelation;
import com.epoch.app.crplatformenhance.superiorsalesorganizationrelation.service.SuperiorSalesOrganizationRelationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/9/3 15:01
 */
@Component
public class OrganizationRelationRepositoryImpl implements OrganizationRelationRepository {

    Log log = Log.getLogger(OrganizationRelationRepositoryImpl.class);

    @Resource
    OrganizationRelationService organizationRelationService;

    @Resource
    SuperiorSalesOrganizationRelationService superiorSalesOrganizationRelationService;

    @Override
    @RepositoryInvoker
    public Boolean addOrganizationRelation(OrganizationRelationSDO sdo) {
        OrganizationRelevanceTypeEnum bizTypeEnum = Arrays.stream(OrganizationRelevanceTypeEnum.values()).filter(a->a.getCode().equals(sdo.getRelevanceType())).findFirst().orElse(null);
        if(bizTypeEnum == null){
            throw new RepositoryException(ErrorMessage.code("OTS-01-003-01-15-015"));
        }
        OrganizationRelation organizationRelation = addOrganizationRelationConverter(sdo);
        organizationRelation.setStatus(CommStatusEnum.ENABLE.getCode());
        organizationRelation.setVersion(1);
        organizationRelation.setTenantId("mengniu");
        CreateOrganizationRelationResponse response = organizationRelationService.createOrganizationRelation(organizationRelation);
        return StringUtil.isNotEmpty(response.getLastInsertId());
    }

    @Override
    @RepositoryInvoker
    public Boolean editOrganizationRelationStatus(OrganizationRelationSDO sdo) {
        OrganizationRelation organizationRelation = editRelationStatusConverter(sdo);
        UpdateOrganizationRelationResponse response = organizationRelationService.updateOrganizationRelation(organizationRelation);
        if(response.getCount()<1){
            log.error("启用或禁用组织关联失败=={}",sdo.toString());
        }
        return true;
    }

    @Override
    @RepositoryInvoker
    public PageResult<List<OrganizationRelationSDO>> getOrganizationRelationByOrgId(IdQueryRequest idQueryRequest) {
        LoadOrganizationRelationListRequest request = new LoadOrganizationRelationListRequest();
        request.setOrganizationId(idQueryRequest.getId());
        request.setOrganizationIds(idQueryRequest.getIds());
        request.setRelevanceType(idQueryRequest.getType());
        request.setRelevanceId(idQueryRequest.getRelevanceId());
        request.setStatus(idQueryRequest.getStatus());
        request.setStart(0);
        request.setLimit(1000);

        Result<List<OrganizationRelation>> result = organizationRelationService.loadOrganizationRelationList(request);
        List<OrganizationRelationSDO> data = result.getResult().stream().map(organiztion -> {
            return organizationRelationSDOConverter(organiztion);
        }).collect(Collectors.toList());
        return PageResult.of(data,result.getTotal());
    }

    @Override
    @RepositoryInvoker
    public PageResult<List<OrganizationRelationSDO>> getOrganizationRelationByRelevanceId(IdQueryRequest idQueryRequest) {
        LoadOrganizationRelationListRequest request = new LoadOrganizationRelationListRequest();
        request.setRelevanceType(idQueryRequest.getType());
        request.setRelevanceId(idQueryRequest.getRelevanceId());
        request.setStatus(idQueryRequest.getStatus());
        request.setStart(0);
        request.setLimit(100);

        Result<List<OrganizationRelation>> result = organizationRelationService.loadOrganizationRelationList(request);
        List<OrganizationRelationSDO> data = result.getResult().stream().map(organiztion -> {
            return organizationRelationSDOConverter(organiztion);
        }).collect(Collectors.toList());
        return PageResult.of(data,result.getTotal());
    }

    @Override
    public Result<List<SuperiorSalesOrganizationRelation>> getSuperiorSalesOrganizationRelation(IdQueryRequest idQueryRequest) {
        SuperiorSalesOrganizationRelationLoadListRequest request = new SuperiorSalesOrganizationRelationLoadListRequest();
        request.setOrgBusinessUnitCode(idQueryRequest.getId());
        request.setOrgBusinessUnitCodeList(idQueryRequest.getIds());
        request.setSuperiorSalesOrganizationCode(idQueryRequest.getRelevanceId());
        request.setStart(0);
        request.setLimit(1000);
        return superiorSalesOrganizationRelationService.loadList(request);
    }

    private OrganizationRelation addOrganizationRelationConverter(OrganizationRelationSDO sdo){
        OrganizationRelation organizationRelation = new OrganizationRelation();
        organizationRelation.setOrganizationId(sdo.getOrganiztionId());
        organizationRelation.setRelevanceId(sdo.getRelevanceId());
        organizationRelation.setRelevanceType(sdo.getRelevanceType());
        organizationRelation.setCreator(sdo.getCreator());
        organizationRelation.setUpdater(sdo.getCreator());
        return organizationRelation;
    }

    private OrganizationRelation editRelationStatusConverter(OrganizationRelationSDO sdo){
        OrganizationRelation organizationRelation = new OrganizationRelation();
        organizationRelation.setId(Long.valueOf(sdo.getId()));
        organizationRelation.setStatus(sdo.getStatus());
        organizationRelation.setVersion(sdo.getVersion());
        return organizationRelation;
    }

    private OrganizationRelationSDO organizationRelationSDOConverter(OrganizationRelation organizationRelation){
        OrganizationRelationSDO organizationRelationSDO = new OrganizationRelationSDO();
        organizationRelationSDO.setId(String.valueOf(organizationRelation.getId()));
        organizationRelationSDO.setOrganiztionId(organizationRelation.getOrganizationId());
        organizationRelationSDO.setRelevanceId(organizationRelation.getRelevanceId());
        organizationRelationSDO.setRelevanceType(organizationRelation.getRelevanceType());
        organizationRelationSDO.setStatus(organizationRelation.getStatus());
        organizationRelationSDO.setVersion(organizationRelation.getVersion());
        return organizationRelationSDO;
    }

}
